#pragma once

#define DIRECTINPUT_VERSION 0x0800
#define KEY_ENTER 12
#define KEY_BACK  10

#include <dinput.h>
#include <Windows.h>
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")

class DInput
{
private:
	POINT					m_CursorPos;
	HWND					m_hWnd;
	IDirectInput8*			m_pDIObject;
	LPDIRECTINPUTDEVICE8	m_pDIKeyboard;
	LPDIRECTINPUTDEVICE8	m_pDIMouse;
	char					m_kbBuffer[256];
	bool					m_kbWasHit[256];
	bool					m_isCaps;			//Whether we're typing in caps or not
	DIMOUSESTATE			m_mouseState;
	bool					m_bInitialized;		// whether DInput::Init() has been successfully called

public:
	DInput();

	bool m_bMouseButtonDown[4];

	bool	Init(HWND hWnd, HINSTANCE hInst);
	bool	PollDevices();
	//bool	AttemptReaquire();
	bool	keyHeld(unsigned char key);		// returns true if the key is being held down
	bool	keyPressed(unsigned char key);	// returns true if a key has been pressed down
	void	resetKey(unsigned char key)
	{
		m_kbBuffer[key] = false;
	}
	bool	mouseButtonDown(int button);
	bool	mouseButtonHit(int button);
	bool	mouseButtonUp(int button);

	char	getLastKeyPress(){
		char	pressed = 0;
		if(keyHeld(DIK_RSHIFT) || keyHeld(DIK_LSHIFT)) 
			m_isCaps = true;

		if (keyPressed(DIK_CAPSLOCK))
			m_isCaps = (m_isCaps?false:true); 

		if(keyPressed(DIK_1))			pressed = '1';
		if(keyPressed(DIK_2))			pressed = '2';
		if(keyPressed(DIK_3))			pressed = '3';
		if(keyPressed(DIK_4))			pressed = '4';
		if(keyPressed(DIK_5))			pressed = '5';
		if(keyPressed(DIK_6))			pressed = '6';
		if(keyPressed(DIK_7))			pressed = '7';
		if(keyPressed(DIK_8))			pressed = '8';
		if(keyPressed(DIK_9))			pressed = '9';
		if(keyPressed(DIK_0))			pressed = '0';
		if(keyPressed(DIK_A))			pressed = 'A';
		if(keyPressed(DIK_B))			pressed = 'B';
		if(keyPressed(DIK_C))			pressed = 'C';
		if(keyPressed(DIK_D))			pressed = 'D';
		if(keyPressed(DIK_E))			pressed = 'E';
		if(keyPressed(DIK_F))			pressed = 'F';
		if(keyPressed(DIK_G))			pressed = 'G';
		if(keyPressed(DIK_H))			pressed = 'H';
		if(keyPressed(DIK_I))			pressed = 'I';
		if(keyPressed(DIK_J))			pressed = 'J';
		if(keyPressed(DIK_K))			pressed = 'K';
		if(keyPressed(DIK_L))			pressed = 'L';
		if(keyPressed(DIK_M))			pressed = 'M';
		if(keyPressed(DIK_N))			pressed = 'N';
		if(keyPressed(DIK_O))			pressed = 'O';
		if(keyPressed(DIK_P))			pressed = 'P';
		if(keyPressed(DIK_Q))			pressed = 'Q';
		if(keyPressed(DIK_R))			pressed = 'R';
		if(keyPressed(DIK_S))			pressed = 'S';
		if(keyPressed(DIK_T))			pressed = 'T';
		if(keyPressed(DIK_U))			pressed = 'U';
		if(keyPressed(DIK_V))			pressed = 'V';
		if(keyPressed(DIK_W))			pressed = 'W';
		if(keyPressed(DIK_X))			pressed = 'X';
		if(keyPressed(DIK_Y))			pressed = 'Y';
		if(keyPressed(DIK_Z))			pressed = 'Z';
		if(keyPressed(DIK_SPACE))		return ' ';
		if(keyPressed(DIK_PERIOD))		return '.';
		if(keyPressed(DIK_RETURN))		return KEY_ENTER;
		if(keyPressed(DIK_BACKSPACE))	return KEY_BACK;

		if(m_isCaps && pressed >= 'A' && pressed <= 'Z')
			pressed += ('a'-'A');			
		return pressed;
	}


	float	mouseDX();
	float	mouseDY();
	float	mouseDZ();
	float	mouseXPos() { return (float)m_CursorPos.x; }
	float	mouseYPos() { return (float)m_CursorPos.y; }
	POINT	mousePos()	{return m_CursorPos;}
	void	Release();
	~DInput();
};